//
//  TimeCacheManager.swift
//  StarTimesON
//
//  Created by Toj on 9/22/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

// MARK: - 当天有效
private let TCDay1ShowedKey = "Day1ShowedKey"
public enum TimeDay1CacheType: String {
    // OTT商品优惠券展示
    case couponAlert
    // 商品退出优惠券弹框
    case couponPromotion
    // 插页广告触发次数重置
    case admobTriggerReset
    
    var key: String {
        return rawValue + TCDay1ShowedKey
    }
}

// MARK: - 长期不会再展示, 可能因为其他因素又可以再次展示
public enum TimeLongCacheType: String {
    // 升级,具体逻辑请查看 DeviceInfo.setForceUpdate
    case upgrade = "AppinfoUpdateVersion"
    
    var key: String {
        return rawValue
    }
}

// MARK: - 只展示一次
private let TCOnlyOneShowedKey = "Only1ShowedKey"
public enum TimeOnly1CacheType: String {
    // 播放器: 声音提示第一次数显示
    case playerVolumeHUD
    // 播放器: 亮度提示第一次数显示
    case playerLuminanceHUD
    
    var key: String {
        return rawValue + TCOnlyOneShowedKey
    }
}

private let keySeparator = "_"

open class TimeCacheManager: NSObject {
    
    public static let manager = TimeCacheManager()
    
    private var todayString: String {
        let today = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let todayString = formatter.string(from: today)
        return todayString
    }
    
    public func clearAll() {
        UserDefaults.standard.set("", forKey: TimeDay1CacheType.couponAlert.key)
        UserDefaults.standard.set("", forKey: TimeDay1CacheType.couponPromotion.key)
        
        UserDefaults.standard.set("", forKey: TimeLongCacheType.upgrade.key)
        
        let key1 = TimeOnly1CacheType.playerLuminanceHUD.key
        let key2 = TimeOnly1CacheType.playerVolumeHUD.key
        let extras = ["vodportrait", "vodlandscape",
                      "liveportrait", "livelandscape"]
        extras.forEach { extra in
            let fullKey1 = key1 + keySeparator + extra
            let fullKey2 = key2 + keySeparator + extra
            UserDefaults.standard.set(false, forKey: fullKey1)
            UserDefaults.standard.set(false, forKey: fullKey2)
        }
        
        UserDefaults.standard.synchronize()
    }
}

// 当天时间缓存
public extension TimeCacheManager {
    
    func setTodayCache(type: TimeDay1CacheType) {
        let value = todayString
        let key = type.key
        UserDefaults.standard.set(value, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    func todayCache(type: TimeDay1CacheType) -> Bool {
        let key = type.key
        let cacheString = UserDefaults.standard.string(forKey: key) ?? ""
        return cacheString == todayString
    }
}

// 缓存只显示1次
public extension TimeCacheManager {
    
    func setToOnly1Cache(type: TimeOnly1CacheType, extra: String = "") {
        var key = type.key
        if !extra.isEmpty { key = key + keySeparator + extra }
        UserDefaults.standard.set(true, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    func only1Cache(type: TimeOnly1CacheType, extra: String = "") -> Bool {
        var key = type.key
        if !extra.isEmpty { key = key + keySeparator + extra }
        let cacheString = UserDefaults.standard.bool(forKey: key)
        return cacheString
    }
}
